/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gp.demo.httclient;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @author Administrator
 */
public class HttpClientProxy {
	static String url = "http://www.0730qc.com/Special/MG5/vote.asp";
	static String loginUrl = "http://www.0730qc.com/Special/MG5/view.asp?id=67#tptp";
	static DefaultHttpClient hc = new DefaultHttpClient();
	static List<Cookie> cookies;

	public static void get() throws ClientProtocolException, IOException {
		HttpGet httpget = new HttpGet(loginUrl);

		HttpResponse response = hc.execute(httpget);
		HttpEntity entity = response.getEntity();

		System.out.println("Login form get: " + response.getStatusLine());
		EntityUtils.consume(entity);

		System.out.println("Initial set of cookies:");
		cookies = hc.getCookieStore().getCookies();
		if (cookies.isEmpty()) {
			System.out.println("None");
		} else {
			for (int i = 0; i < cookies.size(); i++) {
				System.out.println("- " + cookies.get(i).toString());
			}
		}
	}

	public static void post() throws IOException {

		HttpPost pm = new HttpPost(url);
		CookieStore cookieStore = new BasicCookieStore();
		for (Cookie c : cookies) {
			cookieStore.addCookie(c);
		}
		HttpContext localContext = new BasicHttpContext();
		// Bind custom cookie store to the local context
		localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("id", "67"));
		formparams.add(new BasicNameValuePair("ref", "dd"));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
				HTTP.UTF_8);

		pm.setEntity(entity);
		HttpResponse response = hc.execute(pm, localContext);
		HttpEntity entitys = response.getEntity();
		if (null != entitys) {
			InputStream iStream = entitys.getContent();
			Scanner scann = new Scanner(iStream);
			while (scann.hasNextLine()) {
				String info = new String(scann.nextLine().getBytes(), "utf-8");
				System.out.println(info);
			}
			iStream.close();
		}

	}

	public static void main(String[] args) {
		try {
			get();
			post();
			hc.getConnectionManager().shutdown();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
